package com.daiyang.lottery.controller;


import com.daiyang.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.daiyang.lottery.common.exception.ControllerException;
import com.daiyang.lottery.common.pojo.CommonResult;
import com.daiyang.lottery.common.util.JacksonUtil;
import com.daiyang.lottery.controller.param.CreatePrizeParam;
import com.daiyang.lottery.controller.param.PageListParam;
import com.daiyang.lottery.controller.result.FindPrizeListResult;
import com.daiyang.lottery.service.PictureService;
import com.daiyang.lottery.service.PrizeService;


import com.daiyang.lottery.service.dto.PageListDTO;
import com.daiyang.lottery.service.dto.PrizeDTO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@RestController
public class PrizeContoller {
    private static final Logger logger = LoggerFactory.getLogger(PrizeContoller.class);

    @Resource (name = "pictureServiceImpl")
    private PictureService pictureService;

    @Resource(name ="prizeServiceImpl")
    private PrizeService prizeService;
    @RequestMapping("pic/update")
    public String picture(MultipartFile pic) {
       return pictureService.savePicture(pic);
    }
    @RequestMapping("prize/create")
    public CommonResult<Long> creteaPrize(@Valid @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile prizePic) {
        logger.info("CreatePrizeParam:{}", JacksonUtil
                .writeValueAsString(param));
        return CommonResult.success(prizeService.creteaPrize(param, prizePic));
    }
    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageListParam
                                                                   param) {
        logger.info("findPrizeList PageListParam: " +
                JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> prizeListDTO = prizeService.findPrizeList(param);
        return
                CommonResult.success(convertToFindPrizeListResult(prizeListDTO));
    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> prizeListDTO) {
        if(null==prizeListDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.PRIZES_FIND_ERROE);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(prizeListDTO.getTotal());
        result.setRecords(prizeListDTO.getRecords()
                .stream()
                .map(prizeDTO -> {
                    FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                    prizeInfo.setPrizeId(prizeDTO.getId());
                    prizeInfo.setPrizeName(prizeDTO.getName());
                    prizeInfo.setDescription(prizeDTO.getDescription());
                    prizeInfo.setPrice(prizeDTO.getPrice());
                    prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                    return prizeInfo;
                }).collect(Collectors.toList()));
        return result;

    }
}
